function bar2_5D(barHdl, style)
% Copyright (c) 2024, Zhaoxu Liu / slandarer
% 感谢 @明知是意外 的测试，修正了R2023a及之前版本由于CData属性不自动更新导致的上色bug

hold on
CCC = @(x, C1, C2) C2.*sqrt(1 - x.^2) + C1.*(1 - sqrt(1 - x.^2));
Ver = version; Ver = str2double(Ver(1:2));
GraphicsNum = length(get(gca, 'Children')) - length(barHdl);

if nargin < 2
    style = 'w';
end

% 基础属性计算
if Ver < 24
    GroupWidth = 2/3;
else
    GroupWidth = barHdl(1).GroupWidth;
end
if length(barHdl) > 1 && ~strcmp(barHdl(1).BarLayout, 'stacked')
    BarWidth = GroupWidth.*barHdl(1).BarWidth./length(barHdl);
else
    BarWidth = barHdl(1).BarWidth;
end


% 柱状图上色
[XMesh, YMesh] = meshgrid(linspace(0,1,50));
XMesh = 2.*XMesh - 1;
for i = 1:length(barHdl)
    barHdl(i).EdgeColor = 'none';
    
    for j = 1:length(barHdl(i).XEndPoints)
        C = barHdl(i).FaceColor(1,:);
        if strcmp(style, 'w')
            CMesh = cat(3, CCC(XMesh, C(1), .8 + C(1)*.2), ...
                           CCC(XMesh, C(2), .8 + C(2)*.2), ...
                           CCC(XMesh, C(3), .8 + C(3)*.2));
        else
            CMesh = cat(3, CCC(XMesh, .3, C(1)), ...
                           CCC(XMesh, .3, C(2)), ...
                           CCC(XMesh, .3, C(3)));
        end
        if strcmp(barHdl(1).Horizontal, 'on')
            tY = XMesh.*BarWidth./2 + barHdl(i).XEndPoints(j);
            tX = YMesh.*barHdl(i).YData(j) + barHdl(i).YEndPoints(j) - barHdl(i).YData(j);
        else
            tX = XMesh.*BarWidth./2 + barHdl(i).XEndPoints(j);
            tY = YMesh.*barHdl(i).YData(j) + barHdl(i).YEndPoints(j) - barHdl(i).YData(j);
        end
        surfHdl = surf(tX, ...
                       tY, XMesh.*0, ...
                       'CData',CMesh, 'EdgeColor','none');
        surfHdl.Annotation.LegendInformation.IconDisplayStyle = 'off';
        barHdl(i).Annotation.LegendInformation.IconDisplayStyle = 'off';
        % uistack(surfHdl, 'bottom'); % uistack(barHdl(i), 'bottom')
    end
end

for i = 1:length(barHdl), plot([1,1],[1,1], 'Color',barHdl(i).FaceColor(1,:), 'DisplayName',barHdl(i).DisplayName); end
lgdHdl = get(gca, 'Legend');


if ~isempty(lgdHdl)
pause(1e-6)

% 获取图例图标
% lgdEntryChild = lgdHdl.EntryContainer.NodeChildren;
% iconSet = arrayfun(@(lgdEntryChild)lgdEntryChild.Icon.Transform.Children.Children, lgdEntryChild, UniformOutput = false)
childrenList = get(gca, 'Children');
for i = 1:GraphicsNum
    uistack(childrenList(end-length(barHdl)+1-i), 'top');
end
pause(1e-6)
lgdEntryChild = lgdHdl.EntryContainer.NodeChildren;
iconSet = arrayfun(@(lgdEntryChild)lgdEntryChild.Icon.Transform.Children.Children, lgdEntryChild, UniformOutput = false);

XX = [0,1,1,0]; YY = [1,1,0,0];
% 替换图例图标
for j = 1:20
    newFaceHdl = matlab.graphics.primitive.world.Quadrilateral();
    newFaceHdl.PickableParts = 'all';
    newFaceHdl.Layer = 'middle';
    newFaceHdl.ColorBinding = 'object';
    newFaceHdl.ColorType = 'truecoloralpha';
    newFaceHdl.VertexData = single([XX./20 + (j-1)/20; YY; XX.*0]);
    for i = 1:length(barHdl)
        oriEdgeHdl = iconSet{end + 1 - i};
        tNewFaceHdl = copy(newFaceHdl);
        C = oriEdgeHdl.ColorData;
        if strcmp(style, 'w')
            tNewFaceHdl.ColorData = uint8([CCC((j-1)/19*2-1, double(C(1)), 204 + double(C(1)).*.2); ...
                                           CCC((j-1)/19*2-1, double(C(2)), 204 + double(C(2)).*.2); ...
                                           CCC((j-1)/19*2-1, double(C(3)), 204 + double(C(3)).*.2); 255]);
        else
            tNewFaceHdl.ColorData = uint8([CCC((j-1)/19*2-1, 76.5, double(C(1))); ...
                                           CCC((j-1)/19*2-1, 76.5, double(C(2))); ...
                                           CCC((j-1)/19*2-1, 76.5, double(C(3))); 255]);
        end
        tNewFaceHdl.Parent = oriEdgeHdl.Parent;
    end
end
for i = 1:length(barHdl)
    oriEdgeHdl = iconSet{end + 1 - i};
    oriEdgeHdl.Visible = 'off';
end
lgdHdl.AutoUpdate = 'off';
end
end